// pages/center/center.js
const app = getApp()
var request = require("../../utils/request.js");
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    user: {},
    token: '',
    statusBarHeight: app.globalData.statusBarHeight,
    screenHeight: app.globalData.screenHeight,
    windowHeight: 0,
    showBindPhone: false,
    hasPhone: false,
    totalMyTicket: 0,
    userBg: 400,
    dialDialog: false,
    allUserInfo: {},
    timerList: [],
    monthList: [],
    couponList: [],
    verifyLoginDialog: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.createSelectorQuery().selectAll('.mine-title').boundingClientRect(function (rect) {
      that.setData({
        userBg: that.data.statusBarHeight * 2 + 88 + 58 + rect[0].height * 2 + 38 + 28
      })
    }).exec()
    let token = wx.getStorageSync('lmtc_token');
    if (token) {
      that.setData({
        token: token,
        userInfo: wx.getStorageSync('lmtc_userInfo')
      })
      this.initEvent()
    } else {
      this.setData({
        verifyLoginDialog: true
      })
    }
  },
  initEvent() {
    this.getUserInfo();
    this.getUserTimer();
    this.getUserMonth();
    this.getCouponList();
  },
  verifyLogin() {
    this.initEvent()
    this.setData({
      token: wx.getStorageSync('lmtc_token'),
      userInfo: wx.getStorageSync('lmtc_userInfo')
    })
  },
  /**
  * 获取用户时长卡
  */
  getUserTimer() {
    let that = this
    let url = '/customer/info/getCustomerDurationCard'
    request.http(url, 'GET')
      .then(res => {
        if (res.data.code == 200) {
          let time = res.data.data;
          that.setData({
            timerList: time
          })
        }
      })
  },
  /**
   * 获取用户月卡
   */
  getUserMonth() {
    let that = this
    let url = '/customer/info/getCustomerMonthCardList'
    request.http(url, 'GET')
      .then(res => {
        if (res.data.code == 200) {
          let cardList = res.data.data;
          that.setData({
            monthList: cardList
          })
        }
      })
  },
  /**
   * 跳转会员页面
   */
  toDredge() {
    wx.navigateTo({
      url: '../vipCharge/vipCharge?index=1',
    })
  },
  /**
   * 跳转我的服务
   */
  toCenter() {
    wx.navigateTo({
      url: '../grade/grade',
    })
  },
  /**
   * 跳转到我的车辆页面
   */
  toMyCar() {
    let that = this;
    let token = that.data.token;
    if (token == null || token == undefined || token == '') {
      Toast('请先授权');
      return
    }
    wx.navigateTo({
      url: '../licencePlate/licencePlate',
    })
  },
  /**
   * 跳转到停车券
   */
  toParkCoupon() {
    // Toast('正在测试，敬请期待');
    // return
    let that = this;
    let token = that.data.token;
    if (token == null || token == undefined || token == '') {
      Toast('请先授权');
      return
    }
    wx.navigateTo({
      url: '../parkingCoupon/parkingCoupon',
    })
  },
  /**
   * 展示提示绑定手机号的弹窗
   */
  showBindPhone() {
    this.setData({
      showBindPhone: true
    })
  },
  /**
   * 关闭提示绑定手机号的弹窗
   */
  closeBindPop() {
    this.setData({
      showBindPhone: false
    })
  },
  /**
   * 获取用户信息
   * @param {String} userId 用户ID
   */
  getUserInfo() {
    let that = this;
    let url = '/customer/info/getCustomerInfo'
    request.http(url, 'GET')
      .then((res) => {
        if (res.data.code == 200) {
          let user = res.data.data;
          that.setData({
            user: user,
          })
          wx.setStorageSync('lmtc_customerInfo', user)
        } else {
          Toast.fail('获取数据失败');
        }
      })
  },
  /**
   * 获取手机号
   * @param {*} e 
   */
  getPhoneNumber: function (e) {
    var that = this;
    if (e.detail.errMsg == "getPhoneNumber:ok") {
      let encryptedData = e.detail.encryptedData;
      let iv = e.detail.iv;
      wx.login({
        success(res) {
          let url = '/customer/info/bindMobilePhone'
          let params = {
            code: res.code,
            encryptedData: encryptedData,
            iv: iv
          }
          request.http(url, 'GET', params)
            .then(res => {
              if (res.data.code == 200) {
                Toast.success('绑定成功');
                that.setData({
                  'user.mobilePhone': res.data.data,
                  showBindPhone: false
                })
              }
            })
        }
      })
    }
  },
  /**
   * 跳转到预约记录
   */
  toApplyRecord() {
    wx.navigateTo({
      url: '/pages/applayRecord/applayRecord',
    })
  },
  /**
   * 跳转到充值记录
   */
  toChargeRecord() {
    wx.navigateTo({
      url: '/pages/chargeRecord/chargeRecord',
    })
  },
  /**
   * 跳转到订单查询记录
   */
  toOrderRecord() {
    wx.navigateTo({
      url: '/pages/orderRecord/orderRecord',
    })
  },
  /**
   * 跳转到专属服务
   */
  toSkype() {
    wx.navigateTo({
      url: '/pages/vipCharge/vipCharge',
    })
  },
  /**
   * 跳转到我的积分
   */
  toIntegral() {
    Toast('正在测试，敬请期待');
  },
  toAround() {
    Toast('正在测试，敬请期待');
  },
  // 帮助中心
  toHelp() {
    wx.navigateTo({
      url: '/pages/help/help',
    })
  },
  /**
   * 联系客服
   */
  showDialDialog() {
    this.setData({
      dialDialog: true
    })
  },
  /**
   * 关闭联系客服弹窗
   */
  hiddenDialDialog() {
    this.setData({
      dialDialog: false
    })
  },
  /**
   * 拨打电话
   */
  callPhone() {
    wx.makePhoneCall({
      phoneNumber: '0311-89928568',
    })
    this.hiddenDialDialog()
  },
  /**
   * 跳转到个人中心
   */
  toInfomation() {
    wx.navigateTo({
      url: '../information/information'
    })
  },
  bindload(e) {
    console.log('success', e);
  },
  binderror(e) {
    console.log('error', e);
  },
  /**
   * 获取停车券列表
   */
  getCouponList() {
    let that = this;
    let url = '/customer/ticket/getItemTicket'
    request.http(url, 'POST')
      .then((res) => {
        if (res.data.code == 200) {
          that.setData({
            couponList: res.data.data.notExpiredTicketDTOS
          })
        }
      })
  }
})